<?php

/**
 * This is the model class for table "{{distribution}}".
 *
 * The followings are the available columns in table '{{distribution}}':
 * @property string $iddistribution
 * @property string $idfinancialyear
 * @property string $distributiondate
 * @property string $idsociety
 * @property string $idstockitem
 * @property string $distributionquantity
 * @property string $availabilityquantity
 *
 * The followings are the available model relations:
 * @property Financialyear $idfinancialyear0
 * @property Society $idsociety0
 * @property Stockitem $idstockitem0
 */
class Distribution extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Distribution the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{distribution}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idfinancialyear, distributiondate, idsociety, idstockitem', 'required'),
			array('idfinancialyear, idsociety, idstockitem, distributionquantity, availabilityquantity', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('iddistribution, idfinancialyear, distributiondate, idsociety, idstockitem, distributionquantity, availabilityquantity', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idfinancialyear0' => array(self::BELONGS_TO, 'Financialyear', 'idfinancialyear'),
			'idsociety0' => array(self::BELONGS_TO, 'Society', 'idsociety'),
			'idstockitem0' => array(self::BELONGS_TO, 'Stockitem', 'idstockitem'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'iddistribution' => 'Iddistribution',
			'idfinancialyear' => 'Idfinancialyear',
			'distributiondate' => 'Distributiondate',
			'idsociety' => 'Idsociety',
			'idstockitem' => 'Idstockitem',
			'distributionquantity' => 'Distributionquantity',
			'availabilityquantity' => 'Availabilityquantity',
		);
	}

        public $demandquantity;
        public $total;
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria=new CDbCriteria;
                if(isset($_REQUEST['idsociety'])){
                    if(isset(Yii::app()->session['idsociety'])){
                        unset(Yii::app()->session['idsociety']);
                    }
                    Yii::app()->session['idsociety'] = $_REQUEST['idsociety'];
                    $criteria->compare('idsociety',$_REQUEST['idsociety'],true);
                }
                else{
                    if(isset(Yii::app()->session['idsociety'])){
                        $criteria->compare('idsociety',Yii::app()->session['idsociety'],true);
                    }
                }

                if(!isset(Yii::app()->session['distributiondate'])){
        		$criteria->compare('iddistribution',(int) -1,true);
                }
                else{
        		$criteria->compare('distributiondate',Yii::app()->session['distributiondate'],true);
                }
		$criteria->compare('iddistribution',$this->iddistribution,true);
		$criteria->compare('idfinancialyear',$this->idfinancialyear,true);
//		$criteria->compare('distributiondate',$this->distributiondate,true);
		$criteria->compare('idsociety',$this->idsociety,true);
		$criteria->compare('idstockitem',$this->idstockitem,true);
		$criteria->compare('distributionquantity',$this->distributionquantity,true);
		$criteria->compare('availabilityquantity',$this->availabilityquantity,true);

		$dataProvider = new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
                foreach($dataProvider->getData() as $key=>$value){

                    $demandcriteria = new CDbCriteria;
                    $demandcriteria->condition = 'idfinancialyear=:idfinancialyear And idstockitem=:idstockitem And idsociety=:idsociety';
                    $demandcriteria->params = array(
                        ':idfinancialyear' => $value->idfinancialyear,
                        ':idstockitem' => $value->idstockitem,
                        ':idsociety' => $value->idsociety,
                    );
                    $demand = Demand::model()->find($demandcriteria);

                    $value->demandquantity = $demand->demandquantity;
                    $value->total = $value->availabilityquantity - $value->distributionquantity;
//                    echo $value->iddistribution . "<br/>";
                }
//                exit();
                return $dataProvider;
	}
}